<?php
    /**
     *  
     */
    class BaseCategory extends BaseModel
    {
        CONST STATUS_ALL        = -1;
        CONST STATUS_OPEN       = 1;
        CONST STATUS_CLOSE      = 2;
        CONST STATUS_DELETE     = 9;

        protected $_id;
        protected $_keyName;
        protected $_name;
        protected $_description;
        protected $_status;
        protected $_orderedPages;
        protected $_numItems;
        protected $_properties;

        /**
         *  讀取資料表而產生, 請視同cache, 任何情況下皆不可更動該產生的內容
         *  @return Array()
         */
        public static function getTableDefinition()
        {
        }

        /**
         *  經資料表傳至程式中時所對應的 method 資訊
         *  @return Array()
         */
        public static function getTableFields()
        {
            return Array(
                "id"                    => "getId",
                "name"                  => "getName",
                "key_name"              => "getKeyName",
                "description"           => "getDescription",
                "status"                => "getStatus",
                "ordered_pages"         => "getOrderedPages",
                "num_items"             => "getNumItems",
                "properties"            => "getProperties"
            );
        }

        /**
         *  reset to default value model
         *  @return Array()
         */
        public function resetValue() {
            $this->__sleep();
            $this->setId           ( -1 );
            $this->setKeyName      ( NULL );
          //$this->setKeyName      ( md5(rand(0,999999).'_'.time()) );
            $this->setName         ( NULL );
            $this->setDescription  ( NULL );
            $this->setStatus       ( UserServiceCategory::STATUS_OPEN );
            $this->setOrderedPages ( 100 );
            $this->setNumItems     ( 0 );
            $this->setProperties   ( Array() );
        }

        /**
         *  validate model data
         *  @return messages Array()
         */
        public function validate()
        {
            $validateInfo = parent::validate();

            if( !$this->getName() ) {
                $validateInfo['name']['message'] = '不能空值';
            }

            // choose value
            $result = false;
            foreach( $this->getStatusList('status') as $name => $id ) {
                if( $this->getStatus()==$id ) {
                    $result = true;
                    break;
                }
            }
            if(!$result) {
                $validateInfo['status']['message'] = 'status 不正確';
            }

            return $validateInfo;
        }

        /**
         *  filter model data
         */
        public function filter()
        {
            $this->setName         ( trim(strip_tags( $this->getName()        )) );
            $this->setDescription  ( trim(strip_tags( $this->getDescription() )) );
            $this->setStatus       ( (int) $this->getStatus() );
            $this->setOrderedPages ( (int) $this->getOrderedPages() );
            $this->setNumItems     ( (int) $this->getNumItems() );
        }

        /* ------------------------------------------------------------------------------------------------------------------------
            basic method 
        ------------------------------------------------------------------------------------------------------------------------ */

        /**
         *  get KeyName
         *  @return string
         */
        public function getKeyName()
        {
            return $this->_keyName;
        }
        /**
         *  set KeyName
         */
        public function setKeyName( $keyName )
        {
            $this->_keyName = trim($keyName);
        }

        /**
         *  get Name
         *  @return string
         */
        public function getName()
        {
            return $this->_name;
        }
        /**
         *  set Name
         */
        public function setName( $name )
        {
            $this->_name = trim($name);
        }

        /**
         *  get Description
         *  @return string
         */
        public function getDescription()
        {
            return $this->_description;
        }
        /**
         *  set Description
         */
        public function setDescription( $description )
        {
            $this->_description = trim($description);
        }

        /**
         *  get Status 
         *  @return int
         */
        public function getStatus()
        {
            return $this->_status;
        }
        /**
         *  set Status
         */
        public function setStatus( $status )
        {
            $this->_status = (int) $status;
        }

        /**
         *  get OrderedPages 
         *  @return int
         */
        public function getOrderedPages()
        {
            return $this->_orderedPages;
        }
        /**
         *  set OrderedPages
         */
        public function setOrderedPages( $orderedPages )
        {
            $this->_orderedPages = (int) $orderedPages;
        }

        /**
         *  get NumItems
         *  @return int
         */
        public function getNumItems()
        {
            return $this->_numItems;
        }
        /**
         *  set NumItems
         */
        public function setNumItems( $numItems )
        {
            $this->_numItems = (int) $numItems;
        }

        /* ------------------------------------------------------------------------------------------------------------------------
            extends
        ------------------------------------------------------------------------------------------------------------------------ */
        /**
         *  clear extends information
         */
        public function __sleep()
        {
            return parent::__sleep();
        }

    }

